package com.simplemonkey.widgets;

import java.util.Timer;
import java.util.TimerTask;

import com.jme.math.Vector3f;
import com.simplemonkey.IWidget;
import com.simplemonkey.tooltip.ToolTipWidget;

/**
 * A tooltip widget that wait a specified amount of time before showing each tooltip,
 * and a specified amount of time before hiding a tooltip.
 * @author Sam
 *
 */
public class DelayedToolTipWidget extends ToolTipWidget{
	private long displayDelay = 0;
	private long hideDelay = 0;
	
	private IWidget requestOwner = null;
	private String requestToolTip = "";
	private float requestX  = 0;
	private float requestY = 0;
	
	private Timer timer = new Timer(true);
	
	private TimerTask currentShowRequest = null;
	private TimerTask currentHideRequest = null;
	
	
	public DelayedToolTipWidget() {
		this("DelayedToolTip",750, 1000);
	}
	
	public DelayedToolTipWidget(long displayDelay, long hideDelay) {
		this("DelayedToolTip",displayDelay,hideDelay);
	}
	
	/**
	 * 
	 * @param name
	 * @param displayDelay Time to wait before displaying the tooltip (in ms)
	 * @param hideDelay Time to wait before hiding the tooltip (in ms)
	 */
	public DelayedToolTipWidget(String name, long displayDelay, long hideDelay) {
		super(name);
		this.displayDelay = displayDelay;
		this.hideDelay = hideDelay;
	}

	
	
	
	@Override
	public IWidget getInternalWidget(float x, float y) {
		return null;
	}



	@Override
	public IWidget getWidget(float x, float y) {
		return null;
	}

	@Override
	public boolean testCollision(Vector3f position) {
		return false;
	}

	@Override
	public void hideToolTip(IWidget owner) {
		
		if(super.getOwner() == owner || owner == null)
		{
			final IWidget currentOwner = getOwner();
			
			if(this.currentHideRequest != null)
			{
				this.currentHideRequest.cancel();
				this.currentHideRequest = null;
			}
			
	
			
			currentHideRequest = new TimerTask()
			{

				@Override
				public void run() {
					forceHideToolTip(currentOwner);					
					currentHideRequest = null;
				}
				
			};
			
			timer.schedule(currentHideRequest, hideDelay);
		}
		
		if (owner == requestOwner && currentShowRequest!= null)
		{
			currentShowRequest.cancel();
			currentShowRequest = null;
			requestOwner = null;
			//just cancel the request
		}
		//super.hideToolTip(owner);
	}

	@Override
	public void showToolTip(IWidget owner, String toolTip, float x, float y) {
		if(owner != super.getOwner())
		{
			hideToolTip(super.getOwner());//signal that the old owner should be hidden
		}else
		{
			if(this.currentHideRequest != null)
			{
				this.currentHideRequest.cancel();
				this.currentHideRequest = null;
			}
			
			if(this.currentShowRequest != null)
			{
				this.currentShowRequest.cancel();
				this.currentShowRequest = null;
			}
			requestOwner = null;
		
			return;
		}
			
		if (owner == requestOwner)
		{
			if (requestOwner == super.getOwner())
			{//this tooltip is already displayed, so do nothing
				
			}else
			{
				//this request has already been made - update the tooltip, x, and y, but don't do anything else
				requestX = x;
				requestY = y;
				requestToolTip = toolTip;
			}
		}else
		{
			//if there is a pending request in the works, stop it.
			
			if (currentShowRequest != null)
			{
				currentShowRequest.cancel();
				currentShowRequest= null;
				//terminate the request...
			}
			//make a new request to show this object
			requestOwner = owner;
			
			currentShowRequest = new TimerTask()
			{

				@Override
				public void run() {
					forceShowToolTip(requestOwner, requestToolTip,requestX, requestY);
					requestOwner = null;
					currentShowRequest = null;
				}
				
			};
			
			timer.schedule(currentShowRequest, displayDelay);
		
		}
		
	//	super.showToolTip(owner, toolTip, x, y);
	}
	
	public void forceShowToolTip(IWidget owner, String toolTip, float x, float y) 
	{
		if(this.currentHideRequest != null)
		{
			this.currentHideRequest.cancel();
			this.currentHideRequest = null;
		}
		
		if(this.currentShowRequest != null)
		{
			this.currentShowRequest.cancel();
			this.currentShowRequest = null;
		}
		
		super.showToolTip(owner,toolTip,x,y);
	}
	
	public void forceHideToolTip(IWidget owner) 
	{
		super.hideToolTip(owner);
	}
}
